﻿$(document).ready(function() {
    var feedbackSent = false;
    var responseSent = false;
    $('#submit').click(function() {
        if (!feedbackSent && $('#respondFeedbackTextArea').val().length != 0) {
            feedbackSent = true;
            $.ajax({
                type: 'POST',
                url: "/Respond/SaveFeedback",
                data: { questionID: $('#questionID').val(), comment: $('#respondFeedbackTextArea').val() }
            });
        }
        if (responseSent) {
            $.ajax({
                type: 'POST',
                url: "/Respond/DeleteAlphanumericResponse",
                data: { questionID: $('#questionID').val() },
                async: false
            });
        }
        responseSent = true;
        if ($('#respondNumericAnswer').val().length > 0) {
            $.ajax({
                type: 'POST',
                url: "/Respond/SaveAlphanumericResponse",
                data: { questionID: $('#questionID').val(), answer: $('#respondNumericAnswer').val() },
                success: function() {
                    $('#submit').val("Sent");
                    $('#submit').attr("disabled", true);
                }
            });
            $('#respondNumericAnswer').keyup(function() {
                $('#submit').attr("disabled", false);
                $('#submit').val("Submit");
                $('#respondNumericAnswer').unbind('keyup');
            });
        }
        else {
            $('#respondError').text("You have not provided an answer");
            $('#submit').blur(function() {
                $('#respondError').text("");
                $('#submit').unbind('blur');
            });
        }
    });
});